/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/
/* --- Web: www.STCMCU.com --------------------------------------------*/
/* --- Web: www.STCMCUDATA.com  ---------------------------------------*/
/* --- QQ:  800003751 -------------------------------------------------*/
/* ���Ҫ�ڳ�����ʹ�ô˴���,���ڳ�����ע��ʹ����STC�����ϼ�����            */
/*---------------------------------------------------------------------*/

#ifndef	__SPI_H
#define	__SPI_H

#include	"config.h"

//========================================================================
//                               SPI����
//========================================================================

#define		SPI_SSIG_Set(n)					SPCTL = (SPCTL & ~0x80) | (n << 7)		/* SS���Ź��ܿ��� */
#define		SPI_Start(n)						SPCTL = (SPCTL & ~0x40) | (n << 6)		/* SPIʹ�ܿ���λ */
#define		SPI_FirstBit_Set(n)			SPCTL = (SPCTL & ~0x20) | (n << 5)		/* ���ݷ���/����˳�� MSB/LSB */
#define		SPI_Mode_Set(n)					SPCTL = (SPCTL & ~0x10) | (n << 4)		/* SPI����ģʽ���� */
#define		SPI_CPOL_Set(n)					SPCTL = (SPCTL & ~0x08) | (n << 3)		/* SPIʱ�Ӽ��Կ��� */
#define		SPI_CPHA_Set(n)					SPCTL = (SPCTL & ~0x04) | (n << 2)		/* SPIʱ����λ���� */
#define		SPI_Clock_Select(n)			SPCTL = (SPCTL & ~0x03) | (n)					/* SPIʱ��Ƶ��ѡ�� */
#define 	SPI_Interrupt(n)				IE2 = (IE2 & ~0x02) | (n << 1)				/* SPI�ж�ʹ�� */

//========================================================================
//                              ��������
//========================================================================

#define	SPI_BUF_LENTH	128
#define	SPI_BUF_type	__xdata

#define SPI_SS      P11
#define SPI_MOSI    P13
#define SPI_MISO    P14
#define SPI_SCLK    P15

#define SPI_SS_2    P22
#define SPI_MOSI_2  P23
#define SPI_MISO_2  P24
#define SPI_SCLK_2  P25

#define SPI_SS_3    P54
#define SPI_MOSI_3  P40
#define SPI_MISO_3  P41
#define SPI_SCLK_3  P43

#define SPI_SS_4    P35
#define SPI_MOSI_4  P34
#define SPI_MISO_4  P33
#define SPI_SCLK_4  P32

#define	SPI_Mode_Master		1
#define	SPI_Mode_Slave		0
#define	SPI_CPOL_High		1
#define	SPI_CPOL_Low		0
#define	SPI_CPHA_1Edge		0
#define	SPI_CPHA_2Edge		1
#define	SPI_Speed_4			0
#define	SPI_Speed_16		1
#define	SPI_Speed_64		2
#define	SPI_Speed_128		3
#define	SPI_MSB				0
#define	SPI_LSB				1

typedef struct
{
	u8	SPI_Enable;		//ENABLE,DISABLE
	u8	SPI_SSIG;			//ENABLE, DISABLE
	u8	SPI_FirstBit;	//SPI_MSB, SPI_LSB
	u8	SPI_Mode;			//SPI_Mode_Master, SPI_Mode_Slave
	u8	SPI_CPOL;			//SPI_CPOL_High,   SPI_CPOL_Low
	u8	SPI_CPHA;			//SPI_CPHA_1Edge,  SPI_CPHA_2Edge
	u8	SPI_Speed;		//SPI_Speed_4,SPI_Speed_16,SPI_Speed_64,SPI_Speed_128
} SPI_InitTypeDef;


extern	__bit B_SPI_Busy; //����æ��־
extern	u8 	SPI_RxCnt;
extern	u8 	SPI_RxTimerOut;
extern	u8 	SPI_BUF_type SPI_RxBuffer[SPI_BUF_LENTH];


void	SPI_Init(SPI_InitTypeDef *SPIx);
void	SPI_SetMode(u8 mode);
void	SPI_WriteByte(u8 dat);

#endif

